package models

import (
	"sport_booking/dao"
	"sport_booking/utils/message"
)

type Join struct {
	ID          int64  `json:"join_id,omitempty" db:"join_id"`
	CreateStamp int64  `json:"join_create_stamp,omitempty" db:"join_create_stamp"`
	UpdateStamp int64  `json:"join_update_stamp,omitempty" db:"join_update_stamp"`
	State       bool   `json:"join_state" db:"join_state"`
	Note        string `json:"join_note,omitempty" db:"join_note"`
	Activity
	User
}

/*
添加参与信息
*/
func AddJoinInfo(join *Join) (int64, int) {
	sqlStr := "INSERT INTO bk_joins (join_create_stamp,join_update_stamp,join_state,activity_id,user_uuid) VALUES (?,?,?,?,?)"
	result, err := dao.DB.Exec(sqlStr, join.CreateStamp, join.UpdateStamp, join.State, join.Activity.ID, join.UUID)
	if err != nil {
		return 0, message.ErrorJoinAdd
	}
	id, err := result.LastInsertId()
	if err != nil {
		return 0, message.ErrorGetLastInsertId
	}
	if id <= 0 {
		return 0, message.ERROR
	}
	return id, message.SUCCESS
}

/*
根据uuid检查活动id数量
*/
func CheckIdCount(join *Join) int {
	var count int
	sqlStr := "SELECT COUNT(activity_id) FROM bk_joins WHERE activity_id=? AND user_uuid=?"
	err := dao.DB.Get(&count, sqlStr, join.Activity.ID, join.UUID)
	if err != nil {
		return message.ErrorCheckJoin
	}
	if count == 0 {
		return message.SuccessJoinNotExist
	}
	return message.SuccessJoinExist
}

/*
检查参与ID是否存在
*/
func CheckJoinIdState(joinId int64) int {
	var count int
	sqlStr := "SELECT COUNT(join_id) FROM bk_joins WHERE join_id=?"
	err := dao.DB.Get(&count, sqlStr, joinId)
	if err != nil {
		return message.ErrorCheckJoinId
	}
	if count == 0 {
		return message.SuccessJoinIdNotExist
	}
	return message.SuccessJoinIdExist
}

/*
根据uuid修改参与信息
*/
func UpdateJoinInfo(join *Join) int {
	sqlStr := "UPDATE bk_joins SET join_update_stamp=?,join_state=?,join_note=? WHERE user_uuid=? AND join_id=?"
	result, err := dao.DB.Exec(sqlStr, join.UpdateStamp, join.State, join.Note, join.UUID, join.ID)
	if err != nil {
		return message.ErrorUpdateJoin
	}
	row, err := result.RowsAffected()
	if err != nil {
		return message.ErrorGetRowsAffected
	}
	if row == 0 {
		return message.ERROR
	}
	return message.SUCCESS
}

///*
//根据uuid和当前时间戳检查参与修改状态
//*/
//func CheckJoinCount(join *Join) (int, error) {
//	var count int
//	sqlStr := "SELECT COUNT(user_uuid) FROM bk_joins WHERE (user_uuid=? AND activity_id=?) AND ?>join_update_stamp"
//	err := dao.DB.Get(&count, sqlStr, join.User.UUID, join.Activity.ID, join.UpdateStamp)
//	return count, err
//}

/*
根据uuid查看参与活动列表
page 第几页
line 多少行
*/
func GetJoinInfoList(uuid string, next *Next) ([]Join, int) {
	var join []Join
	sqlStr := "SELECT bj.join_id,bj.join_create_stamp,bj.join_update_stamp,bj.join_state,bj.join_note,bj.activity_id,ba.activity_name," +
		"ba.activity_note,ba.activity_address,ba.activity_start_stamp,ba.activity_end_stamp " +
		"FROM bk_joins AS bj,bk_activitys AS ba " +
		"WHERE bj.activity_id=ba.activity_id " +
		"AND bj.user_uuid=? " +
		"ORDER BY bj.join_create_stamp " +
		"LIMIT ?,? "
	err := dao.DB.Select(&join, sqlStr, uuid, (next.Page-1)*next.Line, next.Line)
	if err != nil {
		return nil, message.ErrorGetJoinList
	}
	if join == nil {
		return nil, message.ErrorJoinDataNil
	}
	return join, message.SUCCESS
}
